# TODO: Add comment
# 
# Author: rogb
###############################################################################

setClass("LiborRateFuture",
		representation=list(
				Term="numeric",
				Frequency="character",
				
				Rate="numeric",
				UnderlyingSigma="numeric",
				
				InterestStartDate="Date",
				InterestEndDate="Date"
		),contains=c("SituationDate","MaturityDate","BasicInstrument","Exchange","DayCountConvention","Price")
)

LiborRateFuture <- function(MaturityTerm,MaturityFrequency,Term,Frequency,Currency,Name,SituationDate,DayCountConvention,Exchange,Price,UnderlyingSigma){	
	if(missing(UnderlyingSigma)){
		UnderlyingSigma <- 0
	}
	# MaturityTerm <- 3
	# MaturityFrequency <- "M"
	# Term <- 3
	# Frequency <- "M"
	# Currency <- "CHF"
	# Name <- "Libor Future"
	# SituationDate <- Sys.Date()
	# DayCountConvention <- "30/360"
	# Exchange <- "SWISSEXCHANGE"
	# Price <- 99.55
	# UnderlyingSigma <- 0
	AvailableMaturityMonth <- c(3,6,9,12)
	if(MaturityFrequency=="M"){
		tmpMatDate <- plusMonth(Date=SituationDate,nMonth=MaturityTerm)
	}
	if(MaturityFrequency=="Y"){
		tmpMatDate <- plusYear(SituationDate,MaturityTerm)
	}
	tmpYear <- year(tmpMatDate)
	tmpFutureMatDate <- c(getDateOfNthWeekday(tmpYear-1,AvailableMaturityMonth,3,3),getDateOfNthWeekday(tmpYear,AvailableMaturityMonth,3,3))
	MaturityDate <- max((tmpFutureMatDate[tmpFutureMatDate<=tmpMatDate]))
	if(Currency!="GBP"){
		MaturityDate <- MaturityDate - 2
	}
	
	Rate <- 100 - Price
	
	tmpLibor <- LiborRate(Term,Frequency,Rate,Currency,Name,MaturityDate,DayCountConvention,Exchange)
		
	InterestStartDate <- effectiveDate(tmpLibor)
	InterestEndDate <- maturityDate(tmpLibor)
	
	new("LiborRateFuture",
			Term=Term,
			Frequency=Frequency,
			Rate=Rate,
			UnderlyingSigma=UnderlyingSigma,
			Currency=Currency,
			Name=Name,
			SituationDate=SituationDate,
			MaturityDate=MaturityDate,
			InterestStartDate=InterestStartDate,
			InterestEndDate=InterestEndDate,
			DayCountConvention=DayCountConvention,
			Exchange=Exchange,
			Price=Price)
}

# irf <- LiborRateFuture(MaturityTerm,MaturityFrequency,Term,Frequency,Currency,Name,SituationDate,DayCountConvention,Exchange,Price,UnderlyingSigma)
# object <- lrf 

setMethod("show","LiborRateFuture",function(object){
			cat(object@Currency,object@Term,object@Frequency,"Libor Rate Future as of",as.character(object@SituationDate),"\n")
			cat("Maturity       Date:",as.character(object@MaturityDate),"\n")
			cat("Interest Start Date:",as.character(object@InterestStartDate),"\n")
			cat("Interest End   Date:",as.character(object@InterestEndDate),"\n")
			cat("Price:",round(object@Price,3),"\n")
			cat("Rate :",round(object@Rate,3),"\n")
		})

setMethod("generateCashFlows","LiborRateFuture",function(object){
			CF <- 100 + accruedInterest(object@Rate,object@DayCountConvention,1,object@InterestStartDate,object@InterestEndDate,object@InterestEndDate) 
			CF <- CF + 1/2 * object@UnderlyingSigma^2 * yearFraction(object@SituationDate,object@InterestStartDate) * yearFraction(object@SituationDate,object@InterestEndDate)
			CashFlow(object@Currency,object@Name,c(object@InterestStartDate,object@InterestEndDate),c(-100,CF))
		})


setMethod("presentValue",signature=c("LiborRateFuture","DiscountFactor"),function(object,arg1){
			presentValue(generateCashFlows(object),arg1)
		})

setMethod("presentValue",signature=c("LiborRateFuture","ZeroRateCurve"),function(object,arg1){
			DF <- as.DiscountFactor(arg1)
			presentValue(generateCashFlows(object),DF)
		})


